<?php

/*
 * 团队设置
 *
 * */
namespace App\Http\Controllers\Admin;



use App\Common\CommonHelpers;
use App\Http\Controllers\Controller;
use App\Libraries\Common\CommonFun;
use App\Models\Article;
use App\Models\Robot;
use App\Models\RobotMessage;
use App\Models\RsMaterial;
use App\Models\RsMsginfo;
use App\Models\RsMsgManage;
use App\Models\RsSendingMachineMsg;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class NewsController extends Controller{


    //消息设置列表
    public function index(Request $request){
        return view('admin.news.index');
    }

    //消息设置api
    public function apiList(Request $request){
        //查询消息数据
        $data=RsMsgManage::lists($request->input('page', 1), $request->input('limit', 20), $request->input());
        return response($data ? array_merge(['code' => 0, 'msg' => '查询成功'],  $data) : ['code' => 906, 'msg' => '当前没有更多数据']);

    }

    //消息添加视图
    public function create(Request $request){
        $model=[];
        return view('admin.news.create',['model'=>$model]);
    }
    //消息修改
    public function edit(Request $request,$id){
        $model=[];
        if( $id){
            //查询数据
            $model =RsMsgManage::where(['id'=>$id])->first();
        }


        return view('admin.news.create',['model'=>$model]);
    }

    //消息添加api
    public function createApi(Request $request){
        $data=RsMsgManage::createApi($request->input());
        if($data){
            return CommonHelpers::responseFormat(0,'','添加成功');

        }else{
            return CommonHelpers::responseFormat(0,'','添加失败');

        }
    }

    //消息删除
    public function delete(Request $request){
        if( ! $id= $request->input("id",false)){
            return CommonHelpers::responseFormat(1,'','当前数据失效');

        }else{
            $model=RsMsgManage::where(['id'=>$id])->delete();
            if($model){
                return CommonHelpers::responseFormat(0,'','删除成功');

            }else{
                return CommonHelpers::responseFormat(1,'','删除失败');

            }
        }
    }


    //消息列表数据视图
    public function newsList(){
        return view('admin.news.list');
    }

    //消息列表数据api
    public function newApiList(Request $request){
        $data=RsMsginfo::list($request->input('page', 1), $request->input('limit', 20), $request->input());
        return response($data ? array_merge(['code' => 0, 'msg' => '查询成功'],  $data) : ['code' => 906, 'msg' => '当前没有更多数据']);
    }



    //机器群发消息列表
    public function SendingMsg(Request $request){
        return view('admin.news.sendmsg');
    }

    //机器群发消息api列表
    public function SendApiList(Request $request){
        $data=RsSendingMachineMsg::lists($request->input('page', 1), $request->input('limit', 20), $request->input());
        return response($data ? array_merge(['code' => 0, 'msg' => '查询成功'],  $data) : ['code' => 906, 'msg' => '当前没有更多数据']);

    }


    //机器消息添加视图
    public function SendApicreate(Request $request){
        $model=[];
        return view('admin.news.sendcreate',['model'=>$model]);
    }

    //消息修改
    public function SendApiedit(Request $request,$id){
        $model=[];
        if( $id){
            //查询数据
            $model =RsSendingMachineMsg::where(['id'=>$id])->first();
        }


        return view('admin.news.sendcreate',['model'=>$model]);
    }

    public function SendcreateApi(Request $request){
        $data=RsSendingMachineMsg::createApi($request->input());
        if($data){
            return CommonHelpers::responseFormat(0,'','添加成功');

        }else{
            return CommonHelpers::responseFormat(0,'','添加失败');

        }
    }

    //删除群发消息
    public function SenddelApi(Request $request){
        if( ! $id= $request->input("id",false)){
            return CommonHelpers::responseFormat(1,'','当前数据失效');

        }else{
            $model=RsSendingMachineMsg::where(['id'=>$id])->delete();
            if($model){
                return CommonHelpers::responseFormat(0,'','删除成功');

            }else{
                return CommonHelpers::responseFormat(1,'','删除失败');

            }
        }
    }

    //素材管理
    public function Material(Request $request){
        return view('admin.news.material.index');
    }

    //素材数据管理
    public function apiMaterial(Request $request){
        $data=Article::lists($request->input('page', 1), $request->input('limit', 20), $request->input());
        return response($data ? array_merge(['code' => 0, 'msg' => '查询成功'],  $data) : ['code' => 906, 'msg' => '当前没有更多数据']);
    }

    //素材数据添加
    public function MaterialApicreate(Request $request){
        if($request->wantsJson()){
            if($request->input('category')==""){
                return CommonHelpers::responseFormat(1,'','请输入发送类型');
            }
            if($request->input('test-editormd')=="" && $request->input('category') !=2){
                return CommonHelpers::responseFormat(1,'','请输入内容');
            }

            if($request->input('big_image')=="" && $request->input('category') !=2){
                return CommonHelpers::responseFormat(1,'','请上传图片');
            }

            if($request->input('times')== "" &&  $request->input('type')=="at_date" ){
                return CommonHelpers::responseFormat(1,'','请选择时间');

            }else if($request->input('type')=="every_week" && ($request->input('week_time')== "" || $request->input('week')== "")){
                return CommonHelpers::responseFormat(1,'','请选择时间或周几');
            }else if($request->input('type')=="every_month" && ($request->input('month_time')== "" || $request->input('month')== "")){
                return CommonHelpers::responseFormat(1,'','请选择时间或每月几号');
            }else if($request->input('type')=="every_day" && $request->input('day_time')== ""){
                return CommonHelpers::responseFormat(1,'','请选择时间');
            }
            if($request->input('type')=="at_date"){
                $times=$request->input('times');
            }else if($request->input('type')=="every_day"){
                $times=$request->input('day_time');
            }else if($request->input('type')=="every_week"){
                $times=$request->input('week').":".$request->input('week_time');
            } else if($request->input('type')=="every_month"){
                $times=$request->input('month').":".$request->input('month_time');
            }else if($request->input('type')=="at_current" ){
                $times=date('Y-m-d H:i:s');
            }else{
                $times='';
            }


            $data=[
                'id'=>$request->input("id"),
                'title'=>$request->input('title'),
                'content'=>$request->input("test-editormd",false),
                'time'=>$times,
                'type'=>$request->input('type'),
                'status'=>$request->input('status'),
                'description'=>$request->input('description'),
                'url'=>$request->input('url'),
                'picurl'=>$request->input('big_image',false),
                'category'=>$request->input('category')
            ];

            $data=Article::createApi($data);
            if($data){
                return CommonHelpers::responseFormat(0,'','添加成功');

            }else{
                return CommonHelpers::responseFormat(1,'','添加失败');

            }
        }
        $model=[];
        return view('admin.news.material.create',['model'=>$model]);
    }

    //消息修改
    public function MaterialApiedit(Request $request,$id){
        $model=[];
        if( $id){
            //查询数据
            $model =Article::where(['id'=>$id])->first();
        }
        return view('admin.news.material.create',['model'=>$model]);
    }

    //素材图文数据图片上传
    public function Upload(Request $request){
        $file = $request->file('file');
        $last_img=data_get($request->input(),"last_img",false);

        if($last_img){
            \Storage::disk('uploadimg')->delete(substr(strrchr($last_img, "/"), 1));
        }
        $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';

        $time = date('Ymdhis',time());
        if ($file->isValid()) {
            $originalName = $file->getClientOriginalName();
            $ext = $file->getClientOriginalExtension();
            $realPath = $file->getRealPath();
            $filename = $time. "_".uniqid() . '.' . $ext;
            $bool = Storage::disk('uploadimg')->put($filename, file_get_contents($realPath));
            if($bool){
                $data=[
                    "code"=>0,
                    "msg"=>"",
                    "data"=>[
                        "url"=>$http_type.$_SERVER['HTTP_HOST'].'/storage/img/'.($filename),
                        'src'=>'/storage/img/'.($filename),
                    ]
                ];
                return json_encode( $data );
            }else{
                echo json_encode(['code' => '1', 'url' => '']);

            }
        }else{
            echo json_encode(['code' => '1', 'url' => '']);
        }
    }

    //删除上传图片
    public function UploadDel(Request $request){
        $src=$request->input('src');
        \Storage::disk('uploadimg')->delete(substr(strrchr($src, "/"), 1));
        return CommonHelpers::responseFormat(0,'','操作成功');
    }

    //图文删除
    public function MaterialDel(Request $request){
        $id=$request->post('id');
        if(!$id){
            return CommonHelpers::responseFormat(1,'','参数错误');
        }
        $sql=Article::where(['id'=>$id])->update(['is_del'=>1]);
        if($sql){
            return CommonHelpers::responseFormat(0,'','操作成功');
        }else{
            return CommonHelpers::responseFormat(1,'','删除出错');
        }
    }

//    //查询图文发送记录信息
//    public function robotIndex(Request $request){
//        return view('admin.news.robot.index');
//    }
//
//    //查看发送记录接口
//    public function robotApi(Request $request){
//        $data=Robot::lists($request->input('page', 1), $request->input('limit', 20), $request->input());
//        return response($data ? array_merge(['code' => 0, 'msg' => '查询成功'],  $data) : ['code' => 906, 'msg' => '当前没有更多数据']);
//    }
//
//
//    //消息记录删除
//    public function RobotDel(Request $request){
//        $id=$request->post('id');
//        if(!$id){
//            return CommonHelpers::responseFormat(1,'','参数错误');
//        }
//        $sql=Robot::where(['id'=>$id])->update(['is_del'=>1]);
//        if($sql){
//            return CommonHelpers::responseFormat(0,'','操作成功');
//        }else{
//            return CommonHelpers::responseFormat(1,'','删除出错');
//        }
//    }

    //设置图文推送
    public function RobotMsg(Request $request){
        return view('admin.news.robotmsg.index');
    }
    //查看发送记录接口
    public function RobotMsgApi(Request $request){
        $data=RobotMessage::lists($request->input('page', 1), $request->input('limit', 20), $request->input());

        return response($data ? array_merge(['code' => 0, 'msg' => '查询成功'],  $data) : ['code' => 906, 'msg' => '当前没有更多数据']);
    }

        //消息记录删除
    public function robotMsgDel(Request $request){
        $id=$request->post('id');
        if(!$id){
            return CommonHelpers::responseFormat(1,'','参数错误');
        }
        $sql=RobotMessage::where(['id'=>$id])->update(['is_del'=>1]);
        if($sql){
            return CommonHelpers::responseFormat(0,'','操作成功');
        }else{
            return CommonHelpers::responseFormat(1,'','删除出错');
        }
    }

    //机器人消息推送配置
    public function SendMachine(Request $request){
        return view('admin.news.send.index');
    }
    //机器人api
    public function SendMachineApi(Request $request){
        $data=Robot::lists($request->input('page', 1), $request->input('limit', 20), $request->input());

        return response($data ? array_merge(['code' => 0, 'msg' => '查询成功'],  $data) : ['code' => 906, 'msg' => '当前没有更多数据']);
    }

    //添加数据
    public function SendMachineCreate(Request $request){
        $model=[];
        if($request->wantsJson()){
            $data=[
              'id'=>$request->input('id'),
              'describe'=>$request->input('describe'),
                'webhook'=>$request->input('webhook'),
                'status'=>$request->input('status')
            ];
            $data=Robot::createApi($data);
            if($data){
                return CommonHelpers::responseFormat(0,'','添加成功');

            }else{
                return CommonHelpers::responseFormat(1,'','添加失败');

            }
        }
        return view('admin.news.send.create',['model'=>$model]);
    }

    //编辑数据
    public function SendMachineUpdate(Request $request,$id){
        $model=Robot::query()->where(['id'=>$id])->first();
        return view('admin.news.send.create',['model'=>$model]);
    }

    //删除机器人
    public function SendMachineDel(Request $request){
        $id=$request->post('id');
        if(!$id){
            return CommonHelpers::responseFormat(1,'','参数错误');
        }
        $sql=Robot::query()->where(['id'=>$id])->delete();
        if($sql){
            return CommonHelpers::responseFormat(0,'','操作成功');
        }else{
            return CommonHelpers::responseFormat(1,'','删除出错');
        }
    }
}
